/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.sys.dao.DepartmentMapper;
import com.rf.sys.model.Department;
import com.rf.sys.model.DepartmentExample;
import com.rf.sys.service.DepartmentService;

@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private DepartmentMapper departmentMapper;//注入dao

   	/**
	 * 根据id获取数据
	 * @param departmentId
	 * @return Department
	 */
   	@Override
	public Department getDepartmentByID(String id
) throws Exception{
		// TODO Auto-generated method stub
		return departmentMapper.selectByPrimaryKey(id
);
	}
	
	/**
	 * 根据Department获取数据
	 * @param Department
	 * @return List<Department>
	 */
	@Override
	public List<Department> getDepartment(Department department) throws Exception{
		// TODO Auto-generated method stub
		DepartmentExample example = new DepartmentExample();
		com.rf.sys.model.DepartmentExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<Department> list = departmentMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<Department>
	 */
	@Override
	public List<Department> getDepartment() throws Exception{
		// TODO Auto-generated method stub
		DepartmentExample example = new DepartmentExample();
		com.rf.sys.model.DepartmentExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return departmentMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param Department
	 * @return
	 */
	@Override
	public void addDepartment(Department department) throws Exception{
		// TODO Auto-generated method stub
		departmentMapper.insert(department);
	}

	/**
	 * 修改数据
	 * @param Department
	 * @param
	 */
	@Override
	public void updateDepartment(Department department) throws Exception{
		// TODO Auto-generated method stub
		departmentMapper.updateByPrimaryKeySelective(department);
	}
	
	/**
	 * 删除数据
	 * @param departmentId
	 * @param
	 */
	@Override
	public void deleteDepartment(String id
) throws Exception{
		// TODO Auto-generated method stub
		departmentMapper.deleteByPrimaryKey(id
);
	}

	/**
	 * 获取数据分页
	 * @param Department
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getDepartmentPage(Department department,PageUtils pageUtils) throws Exception{
		DepartmentExample example = new DepartmentExample();
		com.rf.sys.model.DepartmentExample.Criteria criteria= example.createCriteria();
		if(department.getName()!=null && !department.getName().equals("")){
			criteria.andNameLike(department.getName());
		}
		if(department.getContacts()!=null && !department.getContacts().equals("")){
			criteria.andContactsLike(department.getContacts());
		}
		if(department.getState()!=null && !department.getState().equals("")){
			criteria.andStateEqualTo(department.getState());
		}
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<Department> list = departmentMapper.selectByExample(example);
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
}

